<template>
	<view class="parent" v-if="appType === 'app-p'|| appType === 'mp-p'">
		<user-parent />
	</view>
	<view v-else class="container">
		<view v-if="appType === 'app-tch'|| appType === 'mp-tch'" class="area-view" @click="areaShow = true">
			{{tchInfo.area.areaName}}
		</view>
		<view class="user-view">
			<u-image v-if="appType === 'app-tch'|| appType === 'mp-tch'"
				width="120rpx" height="120rpx" shape="circle"
				:src="tchInfo.staff.headImgUrl"
				@click="uploadClick"></u-image>
			<view v-else @click="uploadClick">
				<children-head-view v-if="parentInfo.defaultBaby"  :gender="parentInfo.defaultBaby.gender" :imgUrl="parentInfo.defaultBaby.imgUrl"></children-head-view>
			</view>
			<view class="user-info">
				<view class="fid">
					<text v-if="appType === 'app-tch'|| appType === 'mp-tch'" class="name" @click="debugClick">
						{{tchInfo.staff.realName}}
						
					</text>
					<text v-else-if="parentInfo.defaultBaby" @click="debugClick">
						{{parentInfo.defaultBaby.name + parentInfo.defaultBaby.relation}}
					</text>
					<text></text>
					<text v-if="appType === 'app-tch'|| appType === 'mp-tch'" class="role" @click="show=true">
						{{tchInfo.defaultRole.roleName}}
					</text>
				</view>
				<text class="desc">{{desc}}</text>
			</view>
			<u-icon v-if="appType === 'app-tch'|| appType === 'mp-tch'" class="setting" name="setting" color="#A6A6A6" size="36" @click="settingClick"></u-icon>
		</view>

		<view class="user-item" v-for="(item, index) in menuList" :key="index" @click="itemClick(item)" >
			<text class="title">{{item.menuName}}</text>
			<u-icon name="arrow-right" color="#A6A6A6" :size="18"></u-icon>
		</view>
  
		
		<view v-if="channel === 'xm'">
			<u-button v-if="appType === 'app-p'|| appType === 'mp-p'"
				class="action" @click="exitClick">退出</u-button>
		</view>
		<u-action-sheet :list="helpList" v-model="helpShow" @click="helpClick"></u-action-sheet>
		<u-select v-model="areaShow" @confirm="areaConfirm" :default-value="getDefaultPositionIndex()" :list="areaList" label-name="areaName" value-name="areaId"></u-select>
		<u-select v-model="show" @confirm="roleConfirm" :default-value="getDefaultPositionIndex()" :list="roleList" label-name="roleName" value-name="id"></u-select>
	</view>
</template>

<script>
	import { getAppEdition, getAppEditionByParent } from "@/api/common.js"
	import {
		mapState,
		mapGetters
	} from 'vuex';
	import {
		uploadTeacher,
		uploadChildren,
		setDefaultStaffRole,
		setDefaultStaffArea,
		userDebind
	} from '@/api/user.js'
	import ChildrenHeadView from '@/components/children-head-view.vue'
	import checkUpdate from '@/uni_modules/uni-upgrade-center-app/utils/check-update.js'
	import UserParent from './user-parent.vue'
	export default {
		name: 'UserTch',
		components: {
			ChildrenHeadView,
			UserParent
		},
		data() {
			return {
				show: false,
				roleList: [],
				areaShow: false,
				areaList: [],

				menuList: [],
				menuListT: [
					{
						menuName: '小群体课表',
						url: '/pages_teach/course/early-schedule-page',
					},
					{
						menuName: '托育课表',
						url: '/pages_teach/course/course-list',
					},
					{
						menuName: '我的审核',
						url: '/pages_sub/approval/approval-page',
					},
					// {
					// 	menuName: '园所绩效',
					// 	url: '',
					// },
					{
						menuName: '通讯录',
						url: '/pages_base/common/address-page',
					},
					{
						menuName: '使用帮助',
						url: '/pages_base/charge/index',
					},
				],
				
				menuListP: [
					{
						menuName: '我的服务',
						url: '/pages_base/charge/index',
					},
					{
						menuName: '宝宝信息',
						url: '/pages_base/common/babylist-page',
					},
					{
						menuName: '实时监控',
						url: '/pages_base/common/camera-page',
					},
					// {
					// 	menuName: '积分任务',
					// 	url: '',
					// },
					{
						menuName: '家长满意度',
						url: '/pages_base/common/survey-page',
					},
					{
						menuName: '匿名评分',
						url: '/pages_base/common/staff-rank-page',
					},
					{
						menuName: '使用帮助',
						url: '',
					},
					{
						menuName: '设置',
						url: '/pages_base/common/setting-page'
					}
				],
				menuListP_XM: [
					{
						menuName: '课时余额',
						url: '/pages_base/common/ksye',
					},
					{
						menuName: '我的服务',
						url: '/pages_base/charge/index',
					},
					{
						menuName: '宝宝信息',
						url: '/pages_base/common/babylist-page',
					},
					{
						menuName: '实时监控',
						url: '/pages_base/common/camera-page',
					},
					{
						menuName: '考勤记录',
						url: '/pages_sub/check/attendance',
					},
					{
						menuName: '设置',
						url: '/pages_base/common/setting-page'
					}
				],
				debugCount: 0,
				
				helpList: [{
					text: '常见问题',
				}],
				helpShow: false
			}
		},
		computed: {
			...mapState({
				appType: state => state.app.appType,
				
				tchInfo: state => state.user.tchInfo,
				parentInfo: state => state.user.parentInfo,
			}),
			...mapGetters({
				findMenuList: 'user/findMenuList'
			}),
			
			channel() {
				return getApp().globalData.channel;
			},
			
			desc () {
				if (getApp().globalData.channel === 'xm' ) {
					return '有一种放心叫奈思妈妈'
				}
				if (getApp().globalData.channel === 'xybaby' ) {
					return '紫丹幼儿园陪伴您的每一天'
				}
				if (getApp().globalData.channel === 'jzwy' ) {
					return '中国健康服务业岗位能力提升培训示范基地'
				}
				return '小象湾陪伴您的每一天'
			}
		},
		onLoad() {
			this.init();
			
			if (this.appType === 'app-tch' || this.appType === 'mp-tch') {
				uni.setTabBarItem({
				  index: 0,
				  text: '园区',
				  visible: true
				})
				uni.setTabBarItem({
				  index: 1,
				  text: '班级圈',
				  visible: true
				})
				uni.setTabBarItem({
				  index: 2,
				  text: '我的',
				  visible: true
				})
			} else if (this.appType === 'app-p' || this.appType === 'mp-p') {
				if (this.channel === 'xm') {
					uni.hideTabBar();
					// #ifdef APP-PLUS
						this.checkAppUpdate()
					// #endif
				}
				let tabBar = '在园'
				if (this.channel === 'xxwbaby') {
					tabBar = '在托'
				}
				uni.setTabBarItem({
				  index: 0,
				  text: tabBar,
				  visible: true
				});
				uni.setTabBarItem({
				  index: 1,
				  text: '在家',
				  visible: true
				});
				uni.setTabBarItem({
					index: 2,
					text: '活动通知',
				})
				uni.setTabBarItem({
					index: 3,
					text: '我的',
				})
			}
		},
		methods: {
			init() {
				if (this.appType === 'app-tch' || this.appType === 'mp-tch') {
					this.roleList = this.tchInfo.roles;
					this.areaList = this.tchInfo.areaList;
					this.findMenuList('mobile::tch::tab::user').then(res => {
						console.log(res);
						this.menuList = res.children;
					}).catch(error => {
						console.log(error);
					})
				} else {
					if (this.channel === 'xm') {
						this.menuList = this.menuListP_XM;
					} else {
						this.menuList = this.menuListP;
					}
				}
			},
			
			itemClick(item) {
				// webUni.postMessage({
				// 	data: {
				// 	  action: 'message'
				// 	}
				// });
				// this.$u.route({
				// 	url: '/pages_base/common/camera-page'
				// })
				// return;
				if (item.menuName === '使用帮助') {
					if (this.appType === 'app-tch' || this.appType === 'mp-tch') {
						this.helpShow = true
					} else {
						let url = 'https://res.xxwbaby.com/public/agreement/parent_help.html'
						this.$u.route({
							url: 'pages_base/common/webview',
							params: {
								url: url,
								title: '使用帮助'
							}
						})
					}

				} else {
					this.$u.route({
						url: item.url
					})
				}
			},
			
			uploadClick() {
				let that = this;
				uni.chooseImage({
					count: 1,
					sourceType: ['album', 'camera'],
					sizeType: ['compressed'],
					crop: {
						quality: 80,
						width: 600,
						height: 600,
					},
					success: function(res) {
						console.log(res)
						if (that.appType === 'app-tch' || that.appType === 'mp-tch') {
							that.uploadTeacherHeadImgUrl(res.tempFilePaths[0], res.tempFiles[0]);
						} else if (that.appType === 'app-p' || that.appType === 'mp-p') {
							that.uploadChildrenHeadImgUrl(res.tempFilePaths[0], res.tempFiles[0]);
						}
					}
				});
			},
			uploadTeacherHeadImgUrl(path, file) {
				let fileList = [];
				fileList.push({
					'name': 'file',
					'file': file,
					'uri': path,
				});
				// uni.showLoading({})
				uploadTeacher(path).then(res => {
					console.log(res,'aaa');
					this.$store.commit('user/updateTeacherHeadImgUrl', res.data);
				})
			},
			uploadChildrenHeadImgUrl(path, file) {
				const childrenId = this.parentInfo.defaultBabyId;
				let fileList = [];
				fileList.push({
					'name': 'file',
					'file': file,
					'uri': path,
				});
				// uni.showLoading({})
				uploadChildren(fileList, { childrenId }).then(res => {
					this.$store.commit('user/updateChildrenHeadImgUrl', res.data.imgUrl);
				})
			},
			
			getDefaultPositionIndex() {
				// for (let index in this.roleList) {
				// 	let item = this.roleList[index];
				// 	if (item.id === this.defaultRole.id) {
				// 		console.log(index);
				// 		return [index];
				// 	}
				// }
				return [0];
			},
			roleConfirm(res){
				setDefaultStaffRole(res[0].value).then(res => {
					console.log(res)
					// this.$store.commit('user/setTeacherDefaultRole', res.data);
					this.$store.dispatch('user/refreshTeacherInfo',{
						callback: function() {
							uni.reLaunch({
								url: '/pages/tabbar-item1',
								fail: function(res) {
									console.log(res);
								}
							});
						}
					})
				});
			},
			areaConfirm(res) {
				setDefaultStaffArea(res[0].value).then(res => {
					this.$store.dispatch('user/refreshTeacherInfo',{
						callback: function() {
							uni.reLaunch({
								url: '/pages/tabbar-item1',
								fail: function(res) {
									console.log(res);
								}
							});
						}
					})
				})
			},
			settingClick() {
				this.$u.route({
					url: 'pages_base/common/setting-page'
				})
			},
			
			exitClick() {
				// #ifdef APP-PLUS
					this.logout();
				// #endif
				
				// #ifdef H5
					if(process.env.NODE_ENV === 'development'){
						this.logout();
						return;
					}
					userDebind().then(res => {
						console.log(res);
						this.logout();
					})
				// #endif
			},
			
			logout() {
				this.$store.commit('user/logout');
				uni.reLaunch({
					url: '/pages/login',
					success() {
						
					},
					fail() {
						
					},
				})
			},
			
			debugClick() {
				this.debugCount += 1
				console.log(this.debugCount)
				if (this.debugCount === 10) {
					this.debugCount = 0
					this.$u.route({
						url: 'pages_base/common/debug'
					})
				}
			},
			
			helpClick(index) {
				let url = ''
				if (index === 0) {
					url = 'https://pc.jz1x.com/safe/html/pc_app_help/'
				} else if (index === 1) {
					url = 'https://res.xxwbaby.com/public/agreement/helpVideo/help_video.html'
				}
				this.$u.route({
					url: 'pages_base/common/webview',
					params: {
						url: url,
						title: '使用帮助'
					}
				})
			},
			checkAppUpdate() {
				let appType = '1'
				if(uni.getSystemInfoSync().platform === 'android'){
					const main = plus.android.runtimeMainActivity();
					const pkName = main.getPackageName();
					this.appId = pkName
				} else {
					const packageName = plus.ios.importClass('NSBundle').mainBundle().bundleIdentifier()  
					this.appId = packageName
					appType = '2'
				}
				
				plus.runtime.getProperty(plus.runtime.appid, (widgetInfo) => {
					console.log(widgetInfo)
					this.appVersion = plus.runtime.version
					this.wgtVersion = widgetInfo.version

					this.getAppPackage(this.appId, this.appVersion, this.wgtVersion, appType)
				})
			},
			
			getAppPackage(appId, appVersion, wgtVersion, appType) {
				let getAppVersion = getAppEditionByParent
				if (this.appType === 'app-tch' || this.appType === 'mp-tch') {
					getAppVersion = getAppEdition
				}
				
				getAppVersion({appId, appVersion, wgtVersion, type: appType}).then(res => {
					console.log(res)
					if (res.data) {
						const verionInfo = res.data
						if (res.data.wrapType === 'wgt') {
							uni.downloadFile({
								url: res.data.url,
								success: res => {
									if (res.statusCode == 200) {
										// 下载好直接安装，下次启动生效
										plus.runtime.install(res.tempFilePath, {
											force: false
										});
									}
								}
							});
						} else {
							const PACKAGE_INFO_KEY = '__package_info__'
							const data = {
								appid: appId,
								code: 102,
								contents: verionInfo.content,
								create_date: 1669209718191,
								create_env: "upgrade-center",
								is_mandatory: verionInfo.isMandatory === 1 ? true : false,
								is_silently: false,
								message: "整包更新",
								name: verionInfo.name,
								platform: verionInfo.type === 2 ? ["iOS"] : ["Android"],
								stable_publish: true,
								title: verionInfo.title,
								type: "native_app",
								uni_platform: verionInfo.type === 2 ? "ios" : 'android',
								url: verionInfo.type === 2 ? verionInfo.storeUrl : verionInfo.url,
								version: verionInfo.version,
								_id: "637e1e76819ce86622fefb85"
							}
							console.log(data)
							uni.setStorageSync(PACKAGE_INFO_KEY, data)
							
							uni.navigateTo({
								url: `/uni_modules/uni-upgrade-center-app/pages/upgrade-popup?local_storage_key=${PACKAGE_INFO_KEY}`,
								fail: (err) => {
									console.error('更新弹框跳转失败', err)
									uni.removeStorageSync(PACKAGE_INFO_KEY)
								}
							})
						}
					}
			
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: $app-page-bg-color;
	}
	
	.parent{
		padding-bottom: 160rpx;
	}

	.container {
		padding-left: 30rpx;
		padding-right: 30rpx;
	}
	
	.area-view{
		background: #FFFFFF;
		border-radius: 20rpx;
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		padding-left: 35rpx;
		padding-top: 10rpx;
		padding-bottom: 10rpx;
		padding-right: 35rpx;
		margin-bottom: 30rpx;
		font-size: 24rpx;
		font-family: PingFang SC;
		font-weight: 400;
		color: #333333;
		line-height: 65rpx;
	}

	.user-view {
		position: relative;
		background: #FFFFFF;
		border-radius: 20rpx;
		display: flex;
		flex-direction: row;
		align-items: center;
		padding-left: 35rpx;
		padding-top: 30rpx;
		padding-bottom: 30rpx;
		padding-right: 35rpx;
		margin-top: 30rpx;
		margin-bottom: 30rpx;

		.user-info {
			margin-left: 20rpx;
			display: flex;
			flex-direction: column;

			.fid {
				display: flex;
				flex-direction: row;
				align-items: center;

				.name {
					font-size: 30rpx;
					font-family: PingFang SC;
					font-weight: bold;
					color: #333333;
				}

				.role {
					padding-left: 10rpx;
					padding-right: 10rpx;
					height: 30rpx;
					background: linear-gradient(90deg, #AACD22, #F5AC3C);
					border-radius: 15rpx 15rpx 15rpx 0rpx;
					font-size: 20rpx;
					font-family: PingFang SC;
					font-weight: 400;
					color: #FFFFFF;
					display: flex;
					justify-content: center;
					align-items: center;
					margin-left: 11rpx;
				}
			}

			.desc {
				font-size: 20rpx;
				font-family: PingFang SC;
				font-weight: 400;
				color: #999999;
				margin-top: 15rpx;
			}
		}

		.setting {
			position: absolute;
			right: 35rpx;
			top: 30rpx;
		}
	}

	.user-item {
		background: #FFFFFF;
		border-radius: 20rpx;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		padding-left: 35rpx;
		padding-top: 10rpx;
		padding-bottom: 10rpx;
		padding-right: 35rpx;
		margin-bottom: 30rpx;

		.title {
			font-size: 24rpx;
			font-family: PingFang SC;
			font-weight: 400;
			color: #333333;
			line-height: 65rpx;
		}
	}

	.message {
		width: 81rpx;
		height: 44rpx;
		background: #DA473A;
		border-radius: 22rpx 0rpx 0rpx 22rpx;
		position: absolute;
		top: 80rpx;
		right: 0rpx;

		display: flex;
		justify-content: center;
		align-items: center;

		.icon-view {
			position: relative;
			padding-left: 10rpx;

			.badge {
				position: absolute;
				top: -10rpx;
				left: -10rpx;
				width: 25rpx;
				height: 25rpx;
				background: #FFFFFF;
				border-radius: 50%;
				font-size: 14rpx;
				font-family: Arial;
				font-weight: 400;
				color: #DA473A;
				line-height: 90rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				text-align: center;
			}
		}
	}
</style>
